package setting

import (
	"fmt"
	"net/http"
	"vicente-gin/common"
	"vicente-gin/middleware"
	"vicente-gin/routers"

	"github.com/gin-gonic/gin"
)

func InitRouters() (Router *gin.Engine) {
	//设置模式，设置模式要放在调用Default()函数之前
	//gin.SetMode(gin.ReleaseMode)
	Router = gin.Default()
	Router.Use(middleware.Cors())
	common.LOG.Info("use middleware cors logger recovery")
	//查看模式
	fmt.Println(gin.Mode())

	// Router.Use(middleware.Cors())
	fmt.Println("use middleware cors logger recovery")
	Router.GET("/index", func(c *gin.Context) {
		// RemoteIP() 无代理返回客户端IP,有代理返回代理IP
		fmt.Println(c.RemoteIP())
		ip := c.ClientIP()
		fmt.Println("ip:" + ip)
		clientIp := c.Request.Header.Get("X-Forward-For")
		fmt.Println("clientIp:" + clientIp)
		c.String(http.StatusOK, "Hello, Geektutu")
	})

	//Router.LoadHTMLGlob("templates/*")
	Router.GET("/", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently, "/index")
	})

	ApiV1Group := Router.Group("/api")
	routers.InitUserRouter(ApiV1Group)

	fmt.Println("routers register success")

	return
}
